package com.itheima.health.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.common.R;
import com.itheima.health.mapper.CheckItemDao;
import com.itheima.health.model.pojos.CheckItem;
import com.itheima.health.common.PageParam;
import com.itheima.health.common.ResultPageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    public ResultPageData findPage(PageParam pageParam){
        Integer currentPage = pageParam.getCurrentPage();
        Integer pageSize = pageParam.getPageSize();
        IPage<CheckItem> page = new Page<>(currentPage,pageSize);

        LambdaQueryWrapper<CheckItem> query = new LambdaQueryWrapper<>();

        IPage<CheckItem> pageData = checkItemDao.selectPage(page, query);
        long total = pageData.getTotal();
        List<CheckItem> records = pageData.getRecords();

        ResultPageData resultPageData = new ResultPageData();
        resultPageData.setTotal(total);
        resultPageData.setRows(records);
        return resultPageData;
    }
    /*
     * 检查项新增
     * */
    public R addCheckItem(CheckItem checkItem) {
       int i = checkItemDao.insert(checkItem);
        return R.success(null);
    }
    /*
     * 检查项修改-回显
     * */
    public R findCheckItemById(Integer id) {
        CheckItem checkItem = checkItemDao.selectById(id);
        return R.success(checkItem);
    }
    /*
     * 检查项修改-更新
     * */
    public R updateCheckItem(CheckItem checkItem) {
        int i = checkItemDao.updateById(checkItem);
        return  R.success(null);
    }
    /*
     * 删除检查项
     * */
    public R deleteCheckItem(Integer id) {
        int i = checkItemDao.deleteById(id);
        return R.success(null);
    }
    /*
     * 检查组新增-检查项信息回显
     * */
    public R getCheckGroupItemInfo() {
        List<CheckItem> checkItems = checkItemDao.selectList(null);
        return R.success(checkItems);
    }
    /*
     * 检查组编辑-回显检查组关联的检查项
     * */
    public R findCheckItemIdsByCheckGroupId(Integer id) {
      List<Integer> checkItemIds=  checkItemDao.findCheckItemIdsByCheckGroupId(id);
      return R.success(checkItemIds);
    }

}
